package com.shop.user.api.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.user.model.User;
import com.shop.user.model.search.UserSearchBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    Page<User> findAll(Page<User> page, UserSearchBean bsb);

    default User findById(Integer id) {
        LambdaQueryWrapper<User> qw = Wrappers.lambdaQuery(User.class).eq(User::getId, id);
        return selectOne(qw);
    }

    default boolean updateEnabled(User user) {
        return update(user, Wrappers.lambdaUpdate(User.class)
                .set(User::getEnabled, user.getEnabled())
                .eq(User::getId, user.getId())) == 1;
    }
    default boolean updateLocked(User user) {
        return update(user, Wrappers.lambdaUpdate(User.class)
                .set(User::getLocked, user.getLocked())
                .eq(User::getId, user.getId())) == 1;
    }

    default User findByInput(String username) {
        LambdaQueryWrapper<User> qw = Wrappers.lambdaQuery(User.class).eq(User::getUsername, username);
        return selectOne(qw);
    }

    @Update("update user set user_group_id = #{userGroupId} where id = #{id}")
    int assignUser(User user);
}
